package ext.generic.wfaugment.processors;

import com.ptc.windchill.pdmlink.search.server.processors.SearchFindUsersProcessor;
import com.ptc.core.HTMLtemplateutil.server.processors.UtilProcessorService;
import java.io.*;
import java.util.*;
import wt.fc.EvolvableHelper;
import wt.fc.ReferenceFactory;
import wt.org.*;
import wt.pds.PDSObjectInput;
import wt.templateutil.processor.GenerateFormProcessor;
import wt.util.*;

public class ChooserFormProcessor extends SearchFindUsersProcessor
{
	private static final String CLASSNAME = ChooserFormProcessor.class.getName();
	
	public void userResults(Properties parameters, Locale locale, OutputStream os)
		throws WTException
	{
		System.out.println("goChooserFormProcessor");
		Properties formData = getFormData();
		String idAttr = "";
		if(parameters != null)
		{
			String trailId = parameters.getProperty("trailId");
			idAttr = " id=\"" + trailId + "\"";
		}
		if(formData != null)
		{
			PrintWriter printwriter = getPrintWriter(os, locale);
			String criteria = formData.getProperty("criteria");
			if(criteria != null)
			{
				criteria = criteria.trim();
			}
			if(criteria == null)
			{
				printwriter.println(WTMessage.getLocalizedMessage("com.ptc.windchill.pdmlink.search.server.processors.htmlsearchResource", "33", null, locale) + "<P>");
			} else
			{
				Vector users = userQuery(criteria, locale);
				if(users != null)
				{
					printwriter.println("<P><SELECT" + idAttr + " NAME=\"UserList\" size=10>");
					for(int i = 0; i < users.size(); i++)
					{
						wt.org.WTUser curUser = (wt.org.WTUser)users.elementAt(i);
						printwriter.print("<OPTION value=\"");
						ReferenceFactory rf = new ReferenceFactory();
						printwriter.print(rf.getReferenceString(curUser));
						printwriter.print("\">" + curUser.getName());
						printwriter.println("(" + curUser.getFullName()+")");
					}
					
					printwriter.println("</SELECT>");
					if(users.size() < 1)
					{
						StringBuffer message = new StringBuffer("");
						message.append("<P>");
						message.append(WTMessage.getLocalizedMessage("com.ptc.windchill.pdmlink.search.server.processors.htmlsearchResource", "55", null, locale));
						message.append("</P>");
						printwriter.println(message.toString());
					}
				}
			}
			printwriter.flush();
		}
	}
	
	private Vector userQuery(String criteria, Locale locale)
		throws WTException
	{
		Vector results = new Vector();
		SortedEnumeration usernames = new SortedEnumeration(OrganizationServicesHelper.manager.findLikeUser("fullName", criteria), new PrincipalCollationKeyFactory(locale));
		if(usernames != null)
		{
			WTUser curUser;
			for(; usernames.hasMoreElements(); results.addElement(curUser))
			{
				curUser = (WTUser)usernames.nextElement();
			}
			
		}
		SortedEnumeration fullusernames = new SortedEnumeration(OrganizationServicesHelper.manager.findLikeUser("name", criteria), new PrincipalCollationKeyFactory(locale));
		if(fullusernames != null)
		{
			do
			{
				if(!fullusernames.hasMoreElements())
				{
					break;
				}
				WTUser curUser = (WTUser)fullusernames.nextElement();
				if(!results.contains(curUser))
				{
					results.addElement(curUser);
				}
			} while(true);
		}
		return results;
	}
}
